package com.knife.util;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileWriter;

/**
 * create by yongli on 2021-12-23 10:30
 */

public class FanUpdateUtil {
    public static void main(String[] args) {
        String path = "/Users/yongli/Downloads/风机运维质保信息.xlsx";
        String path2 = "/Users/yongli/Desktop/1224.txt";
        int count = 0;
        try {

            FileWriter fileWriter = new FileWriter(path2);
            Workbook workbook = WorkbookFactory.create(new File(path));
            Sheet sheet = workbook.getSheetAt(0);

            //
            int len = sheet.getPhysicalNumberOfRows();
            System.out.println("len:" + len);

            String template2 = "update  sany_heavy_energy.wf_fan set " +
                    "sany_fan_code = \"arg1\" where code = \"arg2\";";

            for (int i = 1; i < len; i++) {
                Row row = sheet.getRow(i);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                String template = template2;
                if (!row.getCell(1).getStringCellValue().equals("null")) {
                    template = template.replace("arg2", row.getCell(0).getStringCellValue());
                    template = template.replace("arg1", row.getCell(1).getStringCellValue());
                    fileWriter.write(template);
                    fileWriter.write("\n");
                    // 否则部分数据丢失
                    fileWriter.flush();
                    System.out.println(template);

                    count++;
                } else {

//                    System.out.println(row.getCell(1).getStringCellValue());
                }
            }
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(count);
        }

    }
}
